import { createRouter, createWebHistory, RouteOption } from 'vue-router';

export const constantRoutes: RouteOption[] = [
  {
    path: '/',
    component: () => import('@/views/Layout/index.vue'),
    children: [
      { path: '/home', component: () => import('@/views/Home/index.vue') },
      {
        path: '/category/:id',
        component: () => import('@/views/Category/index.vue')
      },
      {
        path: '/goods/:id',
        component: () => import('@/views/Goods/index.vue')
      },
      {
        path: '/cart',
        component: () => import('@/views/Cart/index.vue')
      },
      {
        path: '/checkout',
        component: () => import('@/views/Checkout/index.vue')
      },
      {
        path: '/pay',
        component: () => import('@/views/Pay/index.vue')
      },
      {
        path: '/pay/callback',
        component: () => import('@/views/Pay/callback.vue')
      },
      {
        path: '/member',
        component: () => import('@/views/Member/Layout/index.vue'),
        children: [
          {
            path: '',
            component: () => import('@/views/Member/Home/index.vue')
          },
          {
            path: 'order',
            component: () => import('@/views/Member/Order/index.vue')
          }
        ]
      }
    ]
  },
  { path: '/login', component: () => import('@/views/login/index.vue') },
  {
    path: '/login/callback',
    component: () => import('@/views/login/callback.vue')
  },
  { path: '/test', component: () => import('@/views/Test/index.vue') }
];

/**
 * 创建路由
 */
const router = createRouter({
  history: createWebHistory(),
  routes: constantRoutes,
  // 刷新时，滚动条位置还原
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition;
    } else {
      return { top: 0 };
    }
  }
});

export default router;
